<?php
class ProductPageController extends XPHP_Controller
{
	//Hiển thị menu bên trái trang sản phẩm
	public function leftMenuAction()
	{
		//Lấy danh sách danh mục sản phẩm
		$modelCategories = new Areas_CP_Models_ProductCategories();
		$arrCategories = $modelCategories->select_all(false,"name ASC");
		$this->view->arrCategories = $arrCategories;
		
		//Lấy danh sách sản phẩm nổi bật
		$modelProduct = new Areas_CP_Models_Products();
		$this->view->highlight = $modelProduct->select_all("highlight=1", "date_create DESC");
		return $this->view();
	}
	
	//Phần tìm kiếm
	public function searchAction()
	{
		//Lấy thông tin các danh mục
		$modelCategories = new Areas_CP_Models_ProductCategories();
		$arrCategories = $modelCategories->select_all();
		$modelSizes = new Areas_CP_Models_Sizes();
		$arrSizes = $modelSizes->select_all();
		$this->view->arrSizes = $arrSizes;
		//Các lựa chọn về giá
		$config = XPHP_Registry::get("config");
		$this->view->priceFrom = $config->price->from;
		$this->view->priceTo = $config->price->to;

		return $this->view();
	}
	
	//Phần hỗ trợ trực tuyến
	public function onlineSupportAction()
	{
		//Lấy danh sách hỗ trợ trực tuyến gộp nhóm theo vùng
		$modelOnlineSupport = new Areas_CP_Models_OnlineSupports();
		$modelProvinces = new Areas_CP_Models_Provinces();
		//Lấy danh sách id province có hỗ trợ
		$arrProvinceIDs = $modelOnlineSupport->select_field("distinct provinceID");
		//Lấy mảng danh sách
		$arr = array();
		foreach ($arrProvinceIDs as $provinceID)
		{
			$arr[$modelProvinces->getName($provinceID['provinceID'])] = $modelOnlineSupport->select_all("provinceID=".$provinceID['provinceID']);
		}
		$this->view->arrSupports = $arr;
		return $this->view();
	}
	
	public function onlineSupportAJAX()
	{
		$link = "http://opi.yahoo.com/online?u=".$this->params['yahooID']."&amp;m=a&amp";
		$status = @file($link);
		$status = implode("", $status);
		if(strripos($status, "NOT ONLINE"))
			return $this->json(array("id" => $this->params['yahooID'], "image" => $this->url->content("Content/gmode_newproduct/images/offline.png")));
		elseif (strripos($status, "ONLINE"))
			return $this->json(array("id" => $this->params['yahooID'], "image" => $this->url->content("Content/gmode_newproduct/images/online.png")));
		else 
			return $this->json(array("id" => $this->params['yahooID'], "image" => $this->url->content("Content/gmode_newproduct/images/offline.png")));
	}
	
	
	//Đăng nhập
	public function loginAction()
	{
		return $this->view();
	}
	
	/**
	 * [[
	 * model: Models_Login
	 * ]]
	 */
	public function loginPOST()
	{
		if($this->hasModel())
		{
			if($this->model->isValid())
			{
				if($this->model->login())
				{	
					$customer = $this->model->getMember();
					//Lưu session
					$this->helper->session->member = $customer;
					return $this->json(array("success" => true));
				}
				else
				{
						return $this->json(array("success" => false, "message" => "Tên đăng nhập hoặc mật khẩu không chính xác"));
				}
			}
			else
			{
				return $this->json(array("success" => false, "message" => "Dữ liệu nhập vào không chính xác"));
			}
		}
	}
	
	public function customerPanelAction()
	{
		$model = $this->helper->session->member;
		
		return $this->view($model);
	}
	
	public function logoutAJAX()
	{
		unset($this->helper->session->member);
		
		return $this->json(array("success" => true));
	}
	
	public function currentUrlAction()
	{
		$router = new XPHP_Router();
		$router->analysisRequestUri();
		$currentLink = '<li><span><a href="'.$this->url->action("index", "Index").'">Trang chủ</a></span></li>';
		$modelNewsC = new Areas_CP_Models_NewsCategories();
		$modelProductC = new Areas_CP_Models_ProductCategories();
		if ($router->getController() == "News")
		{
			$args = $this->_router->getArgs();
			if (!empty($args))
			{
				$currentCategory = $modelNewsC->select("id=".$args[0]);
				//Kiểm tra danh mục tin có danh mục con hay không
				if ($currentCategory['parentID'] != 0)
				{
					//Lấy danh mục cha
					$currentLink .= '<li><span><a href="'
						.$this->url->action("index", "News", array($currentCategory['parentID'])).'">' . $modelNewsC->getName($currentCategory['parentID']) . '</a></span></li>';
					$currentLink .= '<li><span><a href="'
						.$this->url->action("index", "News", array($args[0])).'">' . $modelNewsC->getName($args[0]) . '</a></span></li>';
				}
				else
				{
					$currentLink .= '<li><span><a href="'
						.$this->url->action("index", "News", array($args[0])).'">' . $modelNewsC->getName($args[0]) . '</a></span></li>';
				}
			}
			else 
				$currentLink .= '<li><span><a href="'.$this->url->action("index", "News").'">Tin tức</a></span></li>';
		}
		elseif ($router->getController() == "Products")
		{
			$currentLink .= '<li><span><a href="'.$this->url->action("index", "Products").'">Sản phẩm</a></span></li>';
			$args = $this->_router->getArgs();
			//Nếu action: index, kiểm tra args
			if ($router->getAction() == "index")
			{
				if (!empty($args))
					if ($args[0] == "category")
					{
						$currentCategory = $modelProductC->select("id=".$args[1]);
						//Nếu danh mục có danh mục cha
						if ($currentCategory['parentID'] != 0)
						{
							$currentLink .= '<li><span><a href="'
								.$this->url->action("index", "Products", array("category", $currentCategory['parentID'])).'">' 
								. $modelProductC->getName($currentCategory['parentID']) . '</a></span></li>';
							$currentLink .= '<li><span><a href="'
								.$this->url->action("index", "Products", array("category", $args[1])).'">' . $modelProductC->getName($args[1]) 
								. '</a></span></li>';
						}
						else 
						{
							$currentLink .= '<li><span><a href="'
								.$this->url->action("index", "Products", array("category", $args[1])).'">' . $modelProductC->getName($args[1]) 
								. '</a></span></li>';
						}
					}
			}
			elseif ($router->getAction() == "detail")
			{
				$modelProduct = new Areas_CP_Models_Products();
				$product = $modelProduct->select("id='" . $args[0] . "'");
				$currentCategory = $modelProductC->select("id=".$product['categoryID']);
				//Nếu danh mục có danh mục cha
				if ($currentCategory['parentID'] != 0)
				{
					$currentLink .= '<li><span><a href="'
						.$this->url->action("index", "Products", array("category", $currentCategory['parentID'])).'">' 
						. $modelProductC->getName($currentCategory['parentID']) . '</a></span></li>';
					$currentLink .= '<li><span><a href="'
						.$this->url->action("index", "Products", array("category", $currentCategory['id'])).'">' 
						. $modelProductC->getName($currentCategory['id']) 
						. '</a></span></li>';
				}
				else 
				{
					$currentLink .= '<li><span><a href="'
						.$this->url->action("index", "Products", array("category", $currentCategory['id'])).'">' 
						. $modelProductC->getName($currentCategory['id']) 
						. '</a></span></li>';
				}
			}
		}
		elseif ($router->getController() == "Contact") {
			$currentLink .= '<li><span><a href="'.$this->url->action("index", "Contact").'">Liên hệ</a></span></li>';;
		}
		$this->view->currentLink = $currentLink;
		return $this->view();
	}
}